﻿using ProjBackEnd.Areas.Statistics.Models;
using ProjBackEnd.DAL;
using ProjBackEnd.Lib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ProjBackEnd.Areas.Statistics.Controllers
{
    /// <summary>
    /// 业务统计
    /// </summary>
    public class StBizController : ApiController
    {
        /// <summary>
        /// 获取项目统计信息
        /// eg. : 0000000001|TST1|WEB|10|1|||2018-01-01|2018-04-01|0,1,9|
        /// </summary>
        /// <param name="key">memberid|comcd（空值=所有）|登录平台（WEB/PC/APP）|每页记录数|当前页数（从1开始）|项目id（0=所有）|客户id（空值=所有）|项目开始时间（空值=所有）|项目预计结束时间（空值=所有）|Pid（0,1,2,...）空值=所有|负责人id（空值=所有）</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/statistics/stproj")]
        public RespStProj GetStProj(string key)
        {
            RespStProj resp = new RespStProj();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 11)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            string[] strsPid = strsKeyOriginal[9].Split(',');
            string strMgrid = strsKeyOriginal[10];

            int iPageSize, iCurrPage, iCusid, iProjid;
            if (!int.TryParse(strsKeyOriginal[3], out iPageSize) || !int.TryParse(strsKeyOriginal[4], out iCurrPage) || !int.TryParse(strsKeyOriginal[5], out iProjid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            if (iCurrPage == 0)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。当前页码错误";
                return resp;
            }
            int.TryParse(strsKeyOriginal[6], out iCusid);

            DateTime? dtSdt = null;
            DateTime? dtEdt = null;
            try
            {

                if (!string.IsNullOrEmpty(strsKeyOriginal[7])) dtSdt = DateTime.Parse(strsKeyOriginal[7]);
                if (!string.IsNullOrEmpty(strsKeyOriginal[8])) dtEdt = DateTime.Parse(strsKeyOriginal[8]).Date.AddDays(1);
            }
            catch
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }
            if (strsPid.Length == 1)
            {
                if (strsPid[0] == "") strsPid = new string[] { };
            }

            resp = statisticsLogic.GetStProjPaging(strMemberid, strComcd, iPageSize, iCurrPage, iProjid, iCusid, dtSdt, dtEdt, strsPid);
            return resp;
        }

        /// <summary>
        /// 获取业务费用统计信息
        /// eg.：0000000001|COM1|WEB|1|0|0|R|0|0|1
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码|项目id（0=所有）|项目明细id（0=所有；-1=无）|账单id（0=所有）|费用pid（空值=所有）|收付标志（R-收；P-付；空值=所有）|客户编号（0=所有）|合同id（0=所有）|合同子项id（0=所有）|建档人（空值=所有）</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/statistics/stbizfee")]
        public RespStBizFee GetStBizFee(string key)
        {
            RespStBizFee resp = new RespStBizFee();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 12)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            string strPid = strsKeyOriginal[6];
            string strRplb = strsKeyOriginal[7].ToUpper();
            string strCreatid = strsKeyOriginal[11];
            int iProjid, iProjdtlid, iBillid, iCusid, iCtid, iCtSubid;
            if (!int.TryParse(strsKeyOriginal[3], out iProjid)
                || !int.TryParse(strsKeyOriginal[4], out iProjdtlid)
                || !int.TryParse(strsKeyOriginal[5], out iBillid)
                || !int.TryParse(strsKeyOriginal[8], out iCusid)
                || !int.TryParse(strsKeyOriginal[9], out iCtid)
                || !int.TryParse(strsKeyOriginal[10], out iCtSubid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            if (iProjid == 0 && iProjdtlid == 0 && iBillid == 0 && iCusid == 0)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "所有索引编号不能同时为 0";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = statisticsLogic.GetStBizFeeList(strMemberid, strComcd, iProjid, iProjdtlid, iBillid, strPid, strRplb, iCusid, iCtid, iCtSubid, strCreatid);
            return resp;
        }

        /// <summary>
        /// 业务欠费统计
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码|项目id（0=所有）|收付标志（R-收；P-付）|客户编号（0=所有）|结清标志（Y-已结清；N-未结清；空值=所有）</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/statistics/stbizarrears")]
        public RespStBizFee GetStBizArrears(string key)
        {
            RespStBizFee resp = new RespStBizFee();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 7)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            string strRplb = strsKeyOriginal[4].ToUpper();
            string strArrearslb = strsKeyOriginal[6].ToUpper();
            int iProjid, iCusid;
            if (!int.TryParse(strsKeyOriginal[3], out iProjid)
                || !int.TryParse(strsKeyOriginal[5], out iCusid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            if (iProjid == 0 && iCusid == 0)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "所有索引编号不能同时为 0";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = statisticsLogic.GetStBizArrears(strMemberid, strComcd, iProjid, strRplb, iCusid, strArrearslb);
            return resp;
        }


    }
}
